package com.example.myapplication;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Repository for Parental Control settings (time limits and password).
 * Uses SharedPreferences (no database), simple and reliable for settings.
 * Singleton pattern for global access.
 */
public class ParentalControlRepository {

    private static final String PREFS_NAME = "parental_control_prefs";
    private static final String KEY_ENABLE_LIMITS = "enable_limits";
    private static final String KEY_DAILY_LIMIT_MINUTES = "daily_limit_minutes";
    private static final String KEY_BLOCK_START_MINUTE_OF_DAY = "block_start_min"; // 0..1439
    private static final String KEY_BLOCK_END_MINUTE_OF_DAY = "block_end_min";   // 0..1439
    private static final String KEY_PASSWORD = "pc_password";
    private static final String KEY_TEMPORARILY_UNLOCKED = "temp_unlocked";
    private static final String KEY_UNLOCK_TIMESTAMP = "unlock_timestamp";

    private static ParentalControlRepository instance;
    private final SharedPreferences sharedPreferences;

    private ParentalControlRepository(Context context) {
        this.sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }

    /**
     * Get the singleton instance of ParentalControlRepository.
     * @param context Application context
     * @return The singleton instance
     */
    public static synchronized ParentalControlRepository getInstance(Context context) {
        if (instance == null) {
            instance = new ParentalControlRepository(context.getApplicationContext());
        }
        return instance;
    }

    public void setLimitsEnabled(boolean enabled) {
        sharedPreferences.edit().putBoolean(KEY_ENABLE_LIMITS, enabled).apply();
    }

    public boolean isLimitsEnabled() {
        return sharedPreferences.getBoolean(KEY_ENABLE_LIMITS, false);
    }

    public void setDailyLimitMinutes(int minutes) {
        sharedPreferences.edit().putInt(KEY_DAILY_LIMIT_MINUTES, minutes).apply();
    }

    public int getDailyLimitMinutes() {
        return sharedPreferences.getInt(KEY_DAILY_LIMIT_MINUTES, 0);
    }

    public void setBlockTimeRange(int startMinuteOfDay, int endMinuteOfDay) {
        sharedPreferences.edit()
                .putInt(KEY_BLOCK_START_MINUTE_OF_DAY, startMinuteOfDay)
                .putInt(KEY_BLOCK_END_MINUTE_OF_DAY, endMinuteOfDay)
                .apply();
    }

    public int getBlockStartMinuteOfDay() {
        return sharedPreferences.getInt(KEY_BLOCK_START_MINUTE_OF_DAY, -1);
    }

    public int getBlockEndMinuteOfDay() {
        return sharedPreferences.getInt(KEY_BLOCK_END_MINUTE_OF_DAY, -1);
    }

    public void setPassword(String password) {
        sharedPreferences.edit().putString(KEY_PASSWORD, password == null ? "" : password).apply();
    }

    public String getPassword() {
        return sharedPreferences.getString(KEY_PASSWORD, "");
    }

    public void setTemporarilyUnlocked(boolean unlocked) {
        sharedPreferences.edit()
                .putBoolean(KEY_TEMPORARILY_UNLOCKED, unlocked)
                .putLong(KEY_UNLOCK_TIMESTAMP, System.currentTimeMillis())
                .apply();
    }

    public boolean isTemporarilyUnlocked() {
        boolean unlocked = sharedPreferences.getBoolean(KEY_TEMPORARILY_UNLOCKED, false);
        if (!unlocked) {
            return false;
        }

        // 检查解锁是否过期（30分钟）
        long unlockTime = sharedPreferences.getLong(KEY_UNLOCK_TIMESTAMP, 0);
        long currentTime = System.currentTimeMillis();
        long elapsedMinutes = (currentTime - unlockTime) / (1000 * 60);

        if (elapsedMinutes > 30) {
            // 解锁已过期
            setTemporarilyUnlocked(false);
            return false;
        }

        return true;
    }

    public void clearTemporaryUnlock() {
        sharedPreferences.edit()
                .putBoolean(KEY_TEMPORARILY_UNLOCKED, false)
                .remove(KEY_UNLOCK_TIMESTAMP)
                .apply();
    }
}




